/*
' Copyright (c) 2011  DotNetNuke Corporation
'  All rights reserved.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'
*/

using System;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Modules._2DoTasks.Components;
using DotNetNuke.Modules._2DoTasks.Components.Controllers;
using DotNetNuke.Modules._2DoTasks.Components.Entities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace DotNetNuke.Modules._2DoTasks
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View_2DoTasks class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class ProjectList : _2DoTasksModuleBase, IActionable
    {
        #region "Properties"

        /// <summary>
        /// The filter option selected for the projects
        /// </summary>
        private SharedEnum.Project_FilterAditionalOptions FilterOption
        {
            get
            {
                return (SharedEnum.Project_FilterAditionalOptions)Enum.Parse(typeof(SharedEnum.Project_FilterAditionalOptions), drp_filterAditionalOptions.SelectedValue);
            }
        }

        #endregion "Properties"

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    FillControls();

                    if (Project_Created)
                        Messages.ShowSuccess(Localization.GetString("SuccessAddProject", this.LocalResourceFile));
                    else if (Project_Updated)
                        Messages.ShowSuccess(Localization.GetString("SuccessUpdateProject", this.LocalResourceFile));
                    else if (Project_Deleted)
                        Messages.ShowSuccess(Localization.GetString("SuccessDeleteProject", this.LocalResourceFile));
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void lst_projects_LayoutCreated(object sender, EventArgs e)
        {
            (lst_projects.FindControl("lt_Name") as Literal).Text = Localization.GetString("TitleProjectName", this.LocalResourceFile);
            (lst_projects.FindControl("lt_description") as Literal).Text = Localization.GetString("TitleProjectDescription", this.LocalResourceFile);
            (lst_projects.FindControl("lt_endDate") as Literal).Text = Localization.GetString("TitleProjectEndDate", this.LocalResourceFile);
            (lst_projects.FindControl("lt_owner") as Literal).Text = Localization.GetString("TitleOwner", this.LocalResourceFile);
        }

        /// <summary>
        /// Databound for listview project control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lst_projects_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var lnk_edit = e.Item.FindControl("lnk_edit") as LinkButton;

                var curProject = (_Project)e.Item.DataItem;

                //if the user is a manager on the project

                if (IsEditable && lnk_edit != null)
                {
                    lnk_edit.CommandArgument = curProject.ProjectId.ToString();

                    string editUrl = EditUrl(String.Empty, string.Empty, "Edit", "pid=" + curProject.ProjectId.ToString());
                    if (PortalSettings.EnablePopUps)
                    {
                        lnk_edit.Attributes.Add("onclick", "return " + UrlUtils.PopUpUrl(editUrl, this, PortalSettings, true, false));
                    }
                    else
                    {
                        lnk_edit.CommandArgument = editUrl;
                    }
                }

                //else
                //    pnlAdminControls.Visible = false;
            }
        }

        /// <summary>
        /// Rebinds the projects
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drp_filterChange_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                BindProjects();
            }
            catch (Exception exc)
            {
                this.Messages.ShowError(Localization.GetString("ErrorBindProjects", this.LocalResourceFile));

                Exceptions.LogException(exc);
            }
        }

        /// <summary>
        /// Presents the edit control, for editing the selected task
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkEdit_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect((sender as LinkButton).CommandArgument);
        }

        #endregion Event Handlers

        #region "Aux Functions"

        /// <summary>
        /// Fills module controls
        /// </summary>
        private void FillControls()
        {
            FillAditionalOptions();
            BindProjects();
        }

        /// <summary>
        /// Gets the existing projects, and binds them
        /// </summary>
        private void BindProjects()
        {
            //fill existing projects
            using (UnitOfWork uof = new UnitOfWork(PortalId, ModuleId))
            {
                ProjectController ProjectController = new ProjectController(uof);

                var option = FilterOption;
                switch (option)
                {
                    case SharedEnum.Project_FilterAditionalOptions.Archived:
                        lst_projects.DataSource = ProjectController.GetArchived();
                        break;

                    default:
                        lst_projects.DataSource = ProjectController.GetAll();
                        break;
                }

                lst_projects.DataBind();
            }
        }

        /// <summary>
        /// Fills the other filter options control
        /// </summary>
        private void FillAditionalOptions()
        {
            Array enumValues = System.Enum.GetValues(typeof(SharedEnum.Project_FilterAditionalOptions));

            foreach (SharedEnum.Project_FilterAditionalOptions option in enumValues)
            {
                drp_filterAditionalOptions.Items.Add(
                    new ListItem(Localization.GetString(option.ToString(), this.LocalResourceFile),
                                    ((int)option).ToString()));
            }
        }

        #endregion "Aux Functions"

        #region Optional Interfaces

        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion Optional Interfaces
    }
}